﻿using System;
using System.Text;

namespace TFS.Kanban.VS2010.Queries
{
    public class FilterCondition
    {
        private LogicalOperator? _logicalOperator;
        private string _field;
        private WIQLOperator _operator;
        private string _value;

        private static class WIQLOperatorConstants
        {
            public const string Equal = "=";
            public const string NotEqual = "<>";
            public const string GreaterThan = ">";
            public const string LessThan = "<";
            public const string GreaterThanOrEqual = ">=";
            public const string LessThanOrEqual = "<=";
            public const string Contains = "Contains";
            public const string DoesNotContain = "Does Not Contain";
            public const string In = "In";
            public const string InGroup = "In Group";
            public const string NotInGroup = "Not In Group";
            public const string WasEver = "Was Ever";
            public const string Under = "Under";
            public const string NotUnder = "Under";

        }

        private static class LogicalOperatorConstants
        {
            public const string And = "And";
            public const string Or = "Or";
        }

        public FilterCondition(LogicalOperator? logicalOperator, string field, WIQLOperator @operator, string value)
        {
            _logicalOperator = logicalOperator;
            _field = field;
            _operator = @operator;
            _value = value;
        }

        public string GetFilterCondition()
        {
            var result = new StringBuilder();

            var logicalOperator = MapLogicalOperator();

            if(string.IsNullOrEmpty(logicalOperator) == false)
            {
                result.Append(" ");
                result.Append(MapLogicalOperator());
            }

            result.Append(" [");
            result.Append(_field);
            result.Append("] ");
            result.Append(MapWIQLOperator());
            result.Append(" ");
            result.Append("'");
            result.Append(_value);
            result.Append("'");

            return result.ToString();
        }

        private string MapWIQLOperator()
        {
            var result = string.Empty;

            switch (_operator)
            {
                case WIQLOperator.Equal:
                    result = WIQLOperatorConstants.Equal;
                    break;
                case WIQLOperator.NotEqual:
                    result = WIQLOperatorConstants.NotEqual;
                    break;
                case WIQLOperator.GreaterThan:
                    result = WIQLOperatorConstants.GreaterThan;
                    break;
                case WIQLOperator.LessThan:
                    result = WIQLOperatorConstants.LessThan;
                    break;
                case WIQLOperator.GreaterThanOrEqual:
                    result = WIQLOperatorConstants.GreaterThanOrEqual;
                    break;
                case WIQLOperator.LessThanOrEqual:
                    result = WIQLOperatorConstants.LessThanOrEqual;
                    break;
                case WIQLOperator.Contains:
                    result = WIQLOperatorConstants.Contains;
                    break;
                case WIQLOperator.DoesNotContain:
                    result = WIQLOperatorConstants.DoesNotContain;
                    break;
                case WIQLOperator.In:
                    result = WIQLOperatorConstants.In;
                    break;
                case WIQLOperator.InGroup:
                    result = WIQLOperatorConstants.InGroup;
                    break;
                case WIQLOperator.NotInGroup:
                    result = WIQLOperatorConstants.NotInGroup;
                    break;
                case WIQLOperator.WasEver:
                    result = WIQLOperatorConstants.WasEver;
                    break;
                case WIQLOperator.Under:
                    result = WIQLOperatorConstants.Under;
                    break;
                case WIQLOperator.NotUnder:
                    result = WIQLOperatorConstants.NotUnder;
                    break;
            }

            return result;
        }

        private string MapLogicalOperator()
        {
            var result = string.Empty;

            if(_logicalOperator.HasValue)
            {
                switch(_logicalOperator.Value)
                {
                    case LogicalOperator.And:
                        result = LogicalOperatorConstants.And;
                        break;
                    case LogicalOperator.Or:
                        result = LogicalOperatorConstants.Or;
                        break;
                }
            }

            return result;
        }
    }
}
